<?php

// 微信注册
// POST member.wxRegister

namespace app\api\resource\member;
use think\Db;
use think\Loader;
use think\Session;
use app\common\logic\Login as LoginLogic;

class WxRegister
{
    // 方法
    public $_method = ['POST'];
    // 前置
    public $_pre    = ['wxOauth','sms'];
    // 描述
    public $_description = '微信注册';
    // 参数
    public $_param  = [
        'model'        => '模式,0-公众号注册(默认)|1-PC扫码注册',
        'mobile'       => '手机号',
        'role'         => '角色 0-未定义 1-创业者 2-个人天使投资人 3-机构投资人',
        'organization' => '所属投资机构名称',
        'password'     => '密码',
        'confpwd'      => '确认密码',
    ];
    // 返回
    public $_return = [
        201 => '注册成功',
        400 => '该微信号已被注册|该手机号已被注册',
        422 => '请求参数不合法',
        500 => '服务器错误',
    ];

    public function run(&$request)
    {
        $model = $request->post('model/d',0);

        $params['mobile']       = $request->post('mobile/s','','trim');
        $params['role']         = $request->post('role/d',0,'abs');
        $params['organization'] = $request->post('organization/s','','trim');
        $params['unionid']      = Session::get('wechat.unionid');
        $params['openid']       = (0 == $model) ? Session::get('wechat.openid') : '';
        $params['password']     = $request->post('password/s','','trim');
        $params['confpwd']      = $request->post('confpwd/s','','trim');

        $validate = Loader::validate('Member');
        if ( !$validate->scene('wxRegister')->check($params) ) {
            $errMsg = $validate->getError();
            return [422,$errMsg];
        }

        $ret = Db::name('member')->where('unionid',$params['unionid'])->field('id')->find();
        if ( !is_null($ret) ) return [400,'该微信号已被注册'];

        $ret = Db::name('member')->where('mobile',$params['mobile'])->field('id')->find();
        if ( !is_null($ret) ) return [400,'该手机号已被注册'];

        $data['role']         = $params['role'];
        $data['organization'] = $params['organization'];
        $data['unionid']      = $params['unionid'];
        $data['openid']       = $params['openid'];
        $data['mobile']       = $params['mobile'];
        $data['password']     = md5($params['password']);
        $data['status']       = ($params['role'] > 1) ? 9 : 0;
        $num = Db::name('member')->insert($data);
        if ($num > 0) {
            $member = Db::name('member')->where('mobile',$params['mobile'])->field('password',true)->find();

            // 初始化个人信息
            $this->_init_member_info($member['id'],$member['mobile'],$model);

            // 初始化投资喜好
            ($member['role'] > 1) && $this->_init_member_like($member['id']);

            $member['info'] = Db::name('member_info')->where('member_id',$member['id'])->find();

            // 登录处理
            LoginLogic::run($member);

            // 删除Session中短信码记录
            Session::delete('sms');

            return [ 201,[] ];
        } else {
            return 500;
        }
    }

    // 初始化个人信息
    private function _init_member_info($memberId, $mobile, $model = 0)
    {
        $data['member_id'] = $memberId;
        $data['mobile']    = $mobile;

        if (0 == $model) {
            // pc注册,设置默认头像、昵名
            $data['nickname']    = Session::get('wechat.nickname');
            $data['headimgurl']  = Session::get('wechat.headimgurl');
        }

        $num = Db::name('member_info')->insert($data);
        return ($num > 0);
    }

    // 初始化投资喜好
    private function _init_member_like($memberId)
    {
        $data['member_id'] = $memberId;
        $num = Db::name('member_like')->insert($data);
        return ($num > 0);
    }
}
